
import Foundation
//子域名访问计数
func subdomainVisits(_ cpdomains: [String]) -> [String] {
    var result: [String: Int] = [:]
    
    for cpdomain in cpdomains {
        let cpdomainArray = cpdomain.split(separator:" ")
        let visitCount = Int(cpdomainArray.first!)!
        let cpdomainParts = cpdomainArray.last!.split(separator:".")
        for index in 0..<cpdomainParts.count {
//            discuss leetcode com
//            discuss.leetcode.com leetcode.com com
            let cpdomainValue = cpdomainParts[index..<cpdomainParts.count].joined(separator:".")
            if result.keys.contains(cpdomainValue) {
                result[cpdomainValue]! += visitCount
            } else {
                result.updateValue(visitCount, forKey: cpdomainValue)
            }
        }
    }
    return result.map({"\($0.value) \($0.key)"})
}
